/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
      ,setter = layui.setter
  ,form = layui.form;

  //角色管理
  table.render({
    elem: '#LAY-user-back-role'
    ,url: layui.setter.api+'admins/group' //模拟接口
      ,where:{'access_token':layui.data(setter.tableName, {
              key: setter.request.tokenName
          })}
    ,cols: [[
      {field: 'group_id', width: 80, title: 'ID', sort: true}
      ,{field: 'group_name', title: '角色名'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
    ]]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-user-back-role)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此角色？', function(index){
          admin.req({
              url: layui.setter.api+'admins/group'
              ,data: {'id':data.group_id,'opt':'del'}
              ,method:'post'
              ,success: function(data){
                  if(data.code == 0){
                      obj.del();
                  }
              }
          });
        layer.close(index);
      });
    }else if(obj.event === 'edit'){
      admin.popup({
        title: '修改角色'
        ,area: ['500px', '480px']
        ,id: 'LAY-popup-user-add'
        ,success: function(layero, index){
          view(this.id).render('admins/groupedit', data).done(function(){
            form.render(null, 'layuiadmin-form-role');
            
            //监听提交
            form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
                admin.req({
                    url: layui.setter.api+'admins/group'
                    ,data: data.field
                    ,method:'post'
                    ,success: function(data){
                        if(data.code == 0){
                            layui.table.reload('LAY-user-back-role'); //重载表格
                            layer.close(index); //执行关闭
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                });
            });
          });
        }
      });
    }
  });

  exports('useradmin', {})
});